//
//  HWVipGiftBagController.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/10.
//  Copyright © 2021 94Wan. All rights reserved.
//  礼包

import UIKit
import JXSegmentedView
import IQKeyboardManagerSwift
class HWVipGiftBagController: HWBaseViewController {
    var tableView:UITableView!
    var tableViewProtocol = HWVipGiftBagProtocol()
    var header:HWVipGiftBagSearchHeader!
    var httpManager = HWVIPHttpManager()
    var type = -1
    var modelArr = NSMutableArray()
    var keyword = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
    }
}

extension HWVipGiftBagController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        header = HWVipGiftBagSearchHeader()
        view.addSubview(header)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
    }
    
    private func initFrame() -> Void {
            
        header.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(header.snp.bottom)
            make.left.right.equalToSuperview()
        
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}


extension HWVipGiftBagController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWVipGiftBagController {
    func loadData() -> Void {
        httpManager.getVipBagList(typeId: type,keyword: keyword) { models in
            self.tableViewProtocol.models = models
            self.tableView.reloadData()
            
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.removeAllObjects()
                
                self.modelArr.addObjects(from: models)
    
            }
            self.tableViewProtocol.models = self.modelArr as? [HWVipGiftBagModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }

    }
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        self.httpManager.getVipBagListFooter(typeId: type,keyword: keyword) { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWVipGiftBagModel]
            self.tableView.reloadData()
        } failure: { error in
//            if error == "暂无房间"{
//                self.collectionView.mj_footer?.endRefreshingWithNoMoreData()
//            }else{
                self.tableView.mj_footer?.endRefreshing()
//            }
        }
        
    }
}

extension HWVipGiftBagController {
    func addAction() -> Void {
        self.tableViewProtocol.receiveButtonClick = {
            id in
            self.httpManager.receiveVipGiftBag(typeId: self.type, id: id) { dic in
                self.loadData()
                self.view.showHUD(title: dic["message"] as? String)
            } failure: { error in
                
            }

        }
        header.searchGame = {
            name in
            self.view.endEditing(true)
            self.keyword = name
            self.loadData()
        }
        
    }
}

